<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class SettingController extends AdminBaseController
{
    /**
     * 首页
     */
    public function index()
    {

        $value = S('setting');
        //S('setting',NULL);
        if (IS_POST) {
            // print_r($_FILES['file']['error']);exit;
            $data = I('post.');
            //  print_r($data);exit;
            // if($_FILES['file']['error']==0){
            //     $config=array(
            //     'rootPath' => './data/upload/common/',
            //     'savePath' => '',
            //         'maxSize' => 512000,//500K
            //     'saveName'   =>    'site_logo',
            //     'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            //     'autoSub'    =>    false,

            //            );
            //      unlink('./data/'.$value['site_logo']);
            //           $upload = new \Think\Upload($config);//先在本地裁剪
            //           $info=$upload->uploadOne($_FILES['file']);
            //     if ($info) {
            //         $data['site_logo']=$info['savename'];
            //         }else{
            //     $this->error($upload->getError(),U('Admin/Setting/index'));exit;
            //                 }
            // }
            if ($data['bjpk_day']) {
                $data['bjpk_day'] = strtotime($data['bjpk_day']);
                //print_r($a);exit;
            }
            if ($data['bjk8_day']) {
                $data['bjk8_day'] = strtotime($data['bjk8_day']);
                //print_r($a);exit;
            }

            if ($data['twbg_day']) {
                $data['twbg_day'] = strtotime($data['twbg_day']);
                //print_r($a);exit;
            }
            if ($data['gxklsf_day']) {
                $data['gxklsf_day'] = strtotime($data['gxklsf_day']);
                //print_r($a);exit;
            }
            if ($data['is_kaipan']) {
                $data['is_kaipan'] = serialize($data['is_kaipan']);
                //print_r($a);exit;
            }
            // print_r($data);exit;
            $model_setting = D('Base');
            $result        = $model_setting->updateSetting($data);
            if ($result >= 0) {
                S('setting', null);
                $this->success('操作成功', U('Admin/Setting/index'));exit;
            } else {
                $this->error('操作失败');exit;
            }

        }
        //print_r($value);exit;
        $kaipan = unserialize($value['is_kaipan']);
        //print_r($kaipan);exit;
        $this->assign('kaipan', $kaipan);
        $this->assign('setting', $value);
        $this->display();
    }
    /**
     * 自定义导航
     */
    public function nav()
    {
        $id = $_GET['id'];
        if ($id) {
            $nav = M('nav')->where('nav_id=' . $id)->find();
            $this->assign('nav', $nav);
            $this->assign('nav_id', $id);
            //  print_r($nav);exit;
        }

        if (IS_POST) {
            if ($_POST['nav_id'] > 0) {
                $data = I('post.');
                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/nav/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'nav-' . time(),
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {
                        unlink('./data/upload/nav/' . $_POST['old_pic']);
                        $data['nav_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/nav'));exit;
                    }
                }

                $result = M('nav')->where('nav_id=' . $_POST['nav_id'])->save($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/nav'));exit;
                } else {
                    $this->error('操作失败');exit;
                }

            } else {
                $data   = I('post.');
                $mid    = M('nav')->max('id');
                $new_id = $mid + 1;
                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/nav/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'nav-' . $new_id,
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {
                        //unlink('./data/upload/common/'.$value['site_logo']);
                        $data['nav_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/nav'));exit;
                    }
                }
                $result = M('nav')->add($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/nav'));exit;
                } else {
                    $this->error('操作失败');exit;
                }
            }

        }

        $navtop  = M('nav')->where('nav_location=0')->select();
        $mintop  = M('nav')->where('nav_location=1')->select();
        $downtop = M('nav')->where('nav_location=2')->select();
        $this->assign('navtop', $navtop);
        $this->assign('mintop', $mintop);
        $this->assign('downtop', $downtop);
        $this->display();
    }

    public function nav_del()
    {
        //    var_dump(I('id'));exit;
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {
                $dd = M('nav')->where('nav_id=' . $id)->find();
                if ($dd['nav_location'] == 0) {
                    $data = M('collect')->where('nav_id=' . $id)->find();
                    if (!empty($data)) {
                        $data = array('info' => '该导航下有收集，不能删除', 'status' => '0');
                        $this->ajaxReturn($data, 'json');
                    } else {
                        M('category')->where('nav_id=' . $id)->delete();
                    }
                }

                $info = M('nav')->where('nav_id=' . $id)->delete();
                if ($info >= 0) {

                    admin_log('删除导航' . '[' . $id . ']', null);
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }
    }
    /**
     * 自定义导航
     */
    public function adv()
    {
        $id = $_GET['id'];
        if ($id) {
            $banner = M('banner')->where('id=' . $id)->find();
            $this->assign('banner', $banner);
            $this->assign('id', $id);
            //print_r($banner);exit;
        }

        if (IS_POST) {
            if ($_POST['adv_id'] > 0) {
                $data = I('post.');

                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/banner/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'banner-' . time(),
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {
                        unlink('./data/upload/banner/' . $_POST['old_pic']);
                        $data['adv_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/adv'));exit;
                    }
                }
                $result = M('banner')->where('id=' . $_POST['adv_id'])->save($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/adv'));exit;
                } else {
                    $this->error('操作失败');exit;
                }

            } else {
                $data   = I('post.');
                $mid    = M('banner')->max('id');
                $new_id = $mid + 1;
                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/banner/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'banner-' . $new_id,
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {
                        //unlink('./data/upload/common/'.$value['site_logo']);
                        $data['adv_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/adv'));exit;
                    }
                }
                $result = M('banner')->add($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/adv'));exit;
                } else {
                    $this->error('操作失败');exit;
                }
            }

        }

        $bannerall = M('banner')->select();
        $this->assign('bannerall', $bannerall);

        $this->display();
    }
    public function adv_del()
    {
        //    var_dump(I('id'));exit;
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {
                $banner = M('banner')->where('id=' . $id)->find();
                unlink('./data/upload/banner/' . $banner['adv_pic']);

                $info = M('banner')->where('id=' . $id)->delete();
                if ($info >= 0) {
                    admin_log('删除banner' . '[' . $id . ']', null);
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }
    }
    //银行列表
    public function bank()
    {
        $model_bank = D('Base');
        $page       = '20';
        $bank       = $model_bank->getBankList($page);

        $this->assign('bank', $bank);
        $this->display();
    }

    //添加银行
    public function bank_add()
    {
        if (IS_POST) {
            $data = I('post.');
            if ($_FILES['logo']['error'] == 0) {

                $bank_id  = M('bank_list')->max('id');
                $nbank_id = $bank_id + 1;
                $config   = array(
                    'rootPath' => './data/upload/bank-icons/',
                    'savePath' => '',
                    'maxSize'  => 512000, //500K
                    // 'saveName'   =>    array('date','Ym'),
                    'saveName' => 'bank_' . intval($nbank_id),
                    'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                    'autoSub'  => false,
                );
                $upload = new \Think\Upload($config); // 实例化上传类
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['logo']);

                if (!$info) {
// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
// 上传成功 获取上传文件信息
                    $data['logo'] = '/data/upload/bank-icons/' . $info['savename'];
                }
            }
            $result = M('bank_list')->add($data);
            if ($result) {
                admin_log('添加银行[' . $_POST['name'] . ']', 1);
                $this->success('添加银行成功', 'bank');exit;
            } else {
                $this->error('添加银行失败', 'bank');exit;
            }
            // print_r($data);exit;
        }
        $this->display();
    }

    //编辑银行
    public function bank_edit()
    {
        $id = $_GET['id'];
        if (IS_POST) {
            $data = I('post.');
            if ($_FILES['logo']['error'] == 0) {

                $bank = M('bank_list')->where('id=' . $_POST['id'])->find();
                if (!empty($bank['logo'])) {
                    unlink('.' . $bank['logo']);
                }
                $config = array(
                    'rootPath' => './data/upload/bank-icons/',
                    'savePath' => '',
                    'maxSize'  => 512000, //500K
                    // 'saveName'   =>    array('date','Ym'),
                    'saveName' => 'bank_' . intval($_POST['id']),
                    'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                    'autoSub'  => false,
                );
                $upload = new \Think\Upload($config); // 实例化上传类
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['logo']);

                if (!$info) {
// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
// 上传成功 获取上传文件信息
                    $data['logo'] = '/data/upload/bank-icons/' . $info['savename'];
                }
            }
            $result = M('bank_list')->where('id=' . $_POST['id'])->save($data);
            if ($result >= 0) {
                admin_log('编辑银行[' . $_POST['name'] . ']', 1);
                $this->success('编辑银行成功', 'bank');exit;
            } else {
                $this->error('编辑银行失败', 'bank');exit;
            }
            // print_r($data);exit;
        }
        $bank_array = M('bank_list')->where('id=' . $id)->find();

        $this->assign('bank_array', $bank_array);
        $this->assign('id', $id);
        $this->display();
    }

    //修改发布下架状态
    public function bank_show()
    {
        $model_bank = M('bank_list');

        if ($_POST['id']) {
            $condition['id']        = $_POST['id'];
            $update_array['isShow'] = '1';

        }
        if ($_POST['bank_id']) {
            $update_array['isShow'] = '0';
            $condition['id']        = $_POST['bank_id'];
        }
        $info = $model_bank->where($condition)->save($update_array);
        if ($info) {
            admin_log('修改银行状态ID[' . $condition['id'] . ']', 1);
            $data = array('info' => '修改成功', 'status' => '1');
            echo json_encode($data);exit;
        } else {
            $data = array('info' => '修改失败', 'status' => '0');
            echo json_encode($data);exit;
        }

    }

    //删除银行
    public function bank_del()
    {
        $model_bank = M('bank_list');

        if ($_POST['id']) {
            $condition['id'] = $_POST['id'];
            $bank_array      = M('bank_list')->where($condition)->find();
            $info            = $model_bank->where($condition)->delete();
            //echo $model_bank->getlastSql();exit;
            if ($info) {
                unlink('.' . $bank_array['logo']);
                admin_log('删除银行ID[' . $condition['id'] . ']', 1);
                $data = array('info' => '修改成功', 'status' => '1');
                echo json_encode($data);exit;
            } else {
                $data = array('info' => '修改失败', 'status' => '0');
                echo json_encode($data);exit;
            }
        } else {
            $data = array('info' => '参数错误', 'status' => '0');
            echo json_encode($data);exit;
        }

    }
    public function bank_del_all()
    {
        $model_bank = M('bank_list');

        $id = $_POST['id'];
        // print_r($id);exit;
        foreach ($id as $key => $val) {
            $bank_array = M('bank_list')->where('id=' . $val)->find();
            unlink('.' . $bank_array['logo']);
            $info = $model_bank->where('id=' . $val)->delete();
            admin_log('批量删除银行' . '[' . $val . ']', null);
        }
        if ($info) {
            $this->success('删除银行完成', 'bank');exit;
        } else {
            $this->error('删除银行失败', 'bank');exit;
        }

    }

    /**
     * 自定义导航
     */
    public function friend()
    {
        $id = $_GET['id'];
        if ($id) {
            $friend = M('friend')->where('id=' . $id)->find();
            $this->assign('friend', $friend);
            $this->assign('id', $id);
            //print_r($banner);exit;
        }

        if (IS_POST) {
            if ($_POST['friend_id'] > 0) {
                $data = I('post.');

                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/friend/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'friend-' . time(),
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {
                        unlink('./data/upload/friend/' . $_POST['old_pic']);
                        $data['adv_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/friend'));exit;
                    }
                }
                $result = M('friend')->where('id=' . $_POST['friend_id'])->save($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/friend'));exit;
                } else {
                    $this->error('操作失败');exit;
                }

            } else {
                $data   = I('post.');
                $mid    = M('friend')->max('id');
                $new_id = $mid + 1;
                if ($_FILES['file']['error'] == 0) {
                    $config = array(
                        'rootPath' => './data/',
                        'savePath' => 'upload/friend/',
                        'maxSize'  => 512000, //500K
                        'saveName' => 'friend-' . $new_id,
                        'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub'  => false,

                    );
                    $upload = new \Think\Upload($config); //先在本地裁剪
                    $info   = $upload->uploadOne($_FILES['file']);
                    if ($info) {

                        $data['friend_pic'] = $info['savename'];
                    } else {
                        $this->error($upload->getError(), U('Admin/Setting/friend'));exit;
                    }
                }
                $result = M('friend')->add($data);
                if ($result >= 0) {
                    $this->success('操作成功', U('Admin/Setting/friend'));exit;
                } else {
                    $this->error('操作失败');exit;
                }
            }

        }

        $friendall = M('friend')->select();
        $this->assign('friendall', $friendall);

        $this->display();
    }
    public function friend_del()
    {
        //    var_dump(I('id'));exit;
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {
                $friend = M('friend')->where('id=' . $id)->find();
                unlink('./data/upload/friend/' . $friend['friend_pic']);

                $info = M('friend')->where('id=' . $id)->delete();
                if ($info >= 0) {
                    admin_log('删除友情链接' . '[' . $id . ']', null);
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }
    }
    //清除缓存
    public function cache()
    {
        /*循环删除文件及其子文件夹和文件*/
        //echo dirname(CACHE_PATH).DIRECTORY_SEPARATOR.'common~runtime.php' ;exit;
        $this->del_all_dir(TEMP_PATH); //项目临时文件目录
        $this->del_all_dir(CACHE_PATH); //项目模版缓存目录
        $this->del_all_dir(DATA_PATH); //项目数据文件目录
        $this->del_all_dir(dirname(CACHE_PATH) . '/common~runtime.php');
        //del_all_dir(LOG_PATH);//项目日志文件目录
        $this->success('清除缓存成功', U('Admin/Setting/index'));exit;

    }

    /*循环删除文件及其子文件夹和文件，这个写在function.php里面*/
    public function del_all_dir($dir_or_file)
    {
        if (is_dir($dir_or_file)) {
            if ($dh = opendir($dir_or_file)) {
                while (false !== ($item = readdir($dh))) {
                    if ($item != '.' && $item != '..') {
                        if (is_dir($dir_or_file . DIRECTORY_SEPARATOR . $item)) {
                            $this->del_all_dir($dir_or_file . DIRECTORY_SEPARATOR . $item);
                        } else {
                            if (unlink($dir_or_file . DIRECTORY_SEPARATOR . $item)) {
                                //echo '成功删除文件：'. $dir_or_file.DIRECTORY_SEPARATOR.$item.'<br />';
                            }
                        }
                    }
                }
                closedir($dh);
                rmdir($dir_or_file);
            }
        } else if (is_file($dir_or_file) && file_exists($dir_or_file)) {
            unlink($dir_or_file);
        }
    }

}
